package com.songminju.jsong_mvc.controller;

import java.util.List;

import com.songminju.jsong_httpserver.http.HttpRequest;
import com.songminju.jsong_httpserver.http.HttpResponse;

public class HttpHandler implements com.songminju.jsong_httpserver.HttpHandler{

	private FilterManager filterManager = new FilterManager();
	private ControllerManager controllerManager = new ControllerManager();
	
	@Override
	public void process(HttpRequest req, HttpResponse res) {
		FilterChain beforeChain = new FilterChain();
		List<Filter> befores = filterManager.befores();
		for(Filter f:befores) {
			if(f.pattern(req)) {
				f.process(req, res, beforeChain);
			}
			if(beforeChain.isEnd()) {
				beforeChain = null;
				break;
			}
		}
		
		controllerManager.process(req, res);
		res.end();
		
		FilterChain afterChain = new FilterChain();
		List<Filter> afters = filterManager.afters();
		for(Filter f:afters) {
			if(f.pattern(req)) {
				f.process(req, res, afterChain);
			}
			if(afterChain.isEnd()) {
				afterChain = null;
				break;
			}
		}
	}

	public FilterManager getFilterManager() {
		return filterManager;
	}

	public void setFilterManager(FilterManager filterManager) {
		this.filterManager = filterManager;
	}

	public ControllerManager getControllerManager() {
		return controllerManager;
	}

	public void setControllerManager(ControllerManager controllerManager) {
		this.controllerManager = controllerManager;
	}
	
	
}
